package top.gytf.unifiedcall.model.exception;

import cn.hutool.core.lang.Assert;
import lombok.Getter;
import org.springframework.lang.Nullable;
import top.gytf.unifiedcall.model.code.StatusCode;
import top.gytf.unifiedcall.model.code.SymbolCode;

/**
 * 基异常，表示一个能被响应体描述的异常
 */
@Getter
public class BaseException extends RuntimeException {
    /**
     * 状态码，其应该代表一种失败的状态（{@link SymbolCode#isSuccess()}为{@code false}）
     */
    private final StatusCode code;
    /**
     * 携带的描述异常状态的数据
     */
    @Nullable
    private final transient Object data;

    public BaseException(StatusCode code) {
        this(code, (String) null);
    }

    public BaseException(StatusCode code, @Nullable String message) {
        this(code, message, null);
    }

    public BaseException(StatusCode code, Throwable cause) {
        this(code, null, cause);
    }

    public BaseException(StatusCode code, Object data) {
        this(code, null, data);
    }

    public BaseException(StatusCode code, @Nullable String message, @Nullable Throwable cause) {
        this(code, message, null, cause);
    }

    public BaseException(StatusCode code, @Nullable String message, @Nullable Object data) {
        this(code, message, data, null);
    }

    public BaseException(StatusCode code, @Nullable String message, @Nullable Object data, @Nullable Throwable cause) {
        this(code, message, data, cause, true, true);
    }

    public BaseException(StatusCode code, @Nullable String message, @Nullable Object data, @Nullable Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        Assert.isFalse(code.symbol().isSuccess());
        this.code = code;
        this.data = data;
    }
}
